unity实现鼠标经过时ui及物体的变色操作 您所在的位置:网站首页 wpf 操作ui动画会卡 unity实现鼠标经过时ui及物体的变色操作

unity实现鼠标经过时ui及物体的变色操作

2023-04-14 03:24| 来源: 网络整理| 查看: 265

1、实现UI的变色

设置Highlighted Color为鼠标经过时变的颜色(Normal为常态,Pressed为按下时的颜色,Disabled为禁止的颜色)

2、通过代码实现物体的颜色改变 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Cube_change : MonoBehaviour { private Color CubeColor; private Texture CubeTexture; public GameObject objCube; // Use this for initialization void Start () { objCube = GameObject.Find("Cube"); objCube.GetComponent().material.color = Color.blue; } public void OnMouseEnter() { objCube.GetComponent().material.color = Color.red; } public void OnMouseExit() { objCube.GetComponent().material.color = Color.blue; } // Update is called once per frame void Update () { }

//+++++++++++++++++++++++++++

unity5.0之后renderer就不能使用material,需要使用GetComponent来获取 GameObject objcub = GameObject.CreatePrimitive(PrimitiveType.Cube); objcub.AddComponent(); objcub.name = "Cube"; //设置color 使用这个来获取material objcub.GetComponent().material.color = Color.blue;

补充:Unity 实现鼠标滑过UI时触发动画

在有些需求中会遇到,当鼠标滑过某个UI物体上方时,为了提醒用户该物体是可以交互时,我们需要添加一个动效和提示音。这样可以提高产品的体验感。

解决方案

1、给需要有动画的物体制作相应的Animation动画。(相同动效可以使用同一动画复用)

2、给需要有动画的物体添加脚本。脚本如下:

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class OnBtnEnter : MonoBehaviour, IPointerEnterHandler,IPointerExitHandler { //鼠标进入按钮触发音效和动画 public void OnPointerEnter(PointerEventData eventData) { // AudioManager.audioManager.PlayEnterAudio();//这里可以将播放触发提示音放在这里,没有可以提示音可以将该行注释掉 if (gameObject.GetComponent()!=null) { if ( gameObject.GetComponent() .isPlaying) { return; } gameObject.GetComponent().wrapMode = WrapMode.Loop; gameObject.GetComponent().Play(); } } //鼠标离开时关闭动画 public void OnPointerExit(PointerEventData eventData) { if ( gameObject.GetComponent() != null ) { if ( gameObject.GetComponent().isPlaying ) { gameObject.GetComponent().wrapMode = WrapMode.Once; return; } gameObject.GetComponent().Stop(); } } }

补充:unity人物接近时触发事件或动画demo

定义物体GameObject o;

效果:当人物接近物体时,物体触发动画,比如位移

1.创建o的动画km和gm

2.创建空物体 Empty,大小稍微比o大一点,拖入o,用来接受触发判定,防止物体移动过后触发器跟着移动,勾选 is trigger

2.人物控制器

using System.Collections; using System.Collections.Generic; using UnityEngine; public class DoorController : MonoBehaviour { private Animation ani; void Start() { //获取子组件下的第一个组件,再获取子组件animation, //如果是获取自身组件,直接GetComponent() ani = transform.GetChild(0).GetComponent(); } private void OnTriggerEnter(Collider other){ //当物体接触到时则播放animation中的km动画 ani.Play("km"); } private void OnTriggerExit(Collider other){ //当物体接触到时则播放animation中的gm动画 ani.Play("gm"); } void Update() { } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持得得之家。如有错误或未考虑完全的地方,望不吝赐教。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有